package com.lj.bruth.controller;

import cn.hutool.core.date.DateTime;
import com.lj.bruth.model.User;
import com.lj.bruth.model.UserDto;
import com.lj.bruth.model.UserDuXieFenLi;
import com.lj.bruth.service.UserService;
import io.shardingsphere.api.HintManager;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
 * @Classname ShardingJdbcController
 * @Description
 *  1：分表 分库 config中 ShardingPreciseConfiguration.java 是javabean形式做的一个连接数据库分库分表的类
 *  2：config中其他类是策略
 * @Date 2021/1/21 21:01
 * @Created by Administrator
 */

@Controller
@RequestMapping(value="/shardingJdbc")
public class ShardingJdbcController {

    private static Logger logger = Logger.getLogger(ShardingJdbcController.class.getName());

    @Autowired
    private UserService userService;

    /**
     * @Author LJ
     * @Description 注意：分库分表完数据后查询是走所有涉及的表
     * @Date 12:05 2021/1/23
     * @Param [orderId]
     * @return java.util.List<com.lj.bruth.model.User>
     **/
    @RequestMapping(value="/queryUser",method=RequestMethod.GET)
    @ResponseBody
    public List<User> queryUser() {
        List<User>list = userService.queryUser();
        DateTime of = DateTime.of("2029-09-09 11:12:12", "yyyy-MM-dd");
        int i = of.dayOfMonth();
        System.out.println("================"+i);
        return list;
    }

    /**
     * @Author LJ
     * @Description 分库分表 实体类中是逻辑表(表实际不存在)
     * @Date 12:06 2021/1/23
     * @Param [user]
     * @return com.lj.bruth.model.User
     **/
    @RequestMapping(value="/insertUserByShardingJdbc",method=RequestMethod.POST)
    @ResponseBody
    public List<User> insertUserByShardingJdbc(@RequestBody List<User>list) {
        List<User> userNew = null;
        try {
            userNew = userService.insertAndResisUser(list);
            logger.info("存储redis和数据库数据是：userNew");
        }catch (Exception e){
            logger.info("数据处理错误！！！");
        }
        return userNew;
    }

    /**
     * @Author LJ
     * @Description 读写分离(表实际存在)
     * @Date 16:37 2021/1/23
     * @Param [list]
     * @return java.util.List<com.lj.bruth.model.User>
     **/
    @RequestMapping(value="/insertUserByDuXieFenLi",method=RequestMethod.POST)
    @ResponseBody
    public List<UserDuXieFenLi> insertUserByDuXieFenLi(@RequestBody List<UserDuXieFenLi>list) {
        List<UserDuXieFenLi> userNew = null;
        try {
            userNew = userService.insertUserByDuXieFenLi(list);
            logger.info("存储redis和数据库数据是：userNew");
        }catch (Exception e){
           e.printStackTrace();
        }
        return userNew;
    }

    /**
     * @Author LJ
     * @Description hint 强制分库，更多适用于查询
     * @Date 17:24 2021/1/26
     * @Param [dataBaseNum, tableBaseNum]
     * @return java.util.List<com.lj.bruth.model.User>
     **/
    @RequestMapping(value="/getHintData",method=RequestMethod.GET)
    @ResponseBody
    public List<UserDto> getHintData(@RequestParam("dataBaseNum") Long dataBaseNum,@RequestParam("tableBaseNum") Long tableBaseNum) {
        List<User> list = userService.getHintData(dataBaseNum,tableBaseNum);
        List<String> listName = new ArrayList<>(list.size());
        List<UserDto> listDto = new ArrayList<>();
        long orderId;
        String status = null;
        String flag = "false";
        for(int i = 0;i<list.size();i++){
            UserDto u = new UserDto();
            orderId = list.get(i).getOrderId();
            status = list.get(i).getStatus();
           for (int j = 0;j < list.size();j++){
              long orderIdJ = list.get(j).getOrderId();
              String statusJ = list.get(j).getStatus();
                 if (orderId == orderIdJ && !status.equals(statusJ)){
                     listName.add(status);
                     listName.add(statusJ);
                     flag = "true";
                 }
           }
            if ("true".equals(flag)){
                u.setStatus(listName);
                u.setOrderId(orderId);
                listDto.add(u);
            }
            if (!"true".equals(flag)){
                listName.add(status);
                u.setStatus(listName);
                u.setOrderId(orderId);
                listDto.add(u);
            }
        }
        //去重，除去相同用户名
        List<UserDto> collect = listDto.stream().distinct().collect(Collectors.toList());
        return collect;
    }


}
